<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <!-- 弹层放置 -->
    <el-form ref="addDept" label-width="100px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="2-10个字符" style="width:80%;" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" placeholder="2-10个字符" style="width:80%;" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width:80%;">
          <el-option v-for="item in managerList" :key="item.id" :value="item.id" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" :rows="4" placeholder="1-100个字符" style="width:80%;" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" @click="btnAsure">确定</el-button>
            <el-button @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { _addDepartment, _getDepartmentDetail, _getDepartmentList, _getManagerList, _updateDepartment } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    },
    showTitle: {
      type: String,
      default: '新增部门'
    }
  },
  data() {
    return {
      managerList: [],
      formData: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      rules: {
        code: [{
          required: true,
          message: '部门编码不能为空',
          trigger: 'blur'
        }, {
          trigger: 'blur',
          message: '部门编码的长度应为2-10个字符',
          min: 2,
          max: 10
        }, {
          trigger: 'blur',
          validator: async(rule, value, callback) => {
            let res = await _getDepartmentList()
            // 判断是否处于编辑模式
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.code === value)) {
              callback(new Error('部门中已有该编码'))
              // console.log('0')
            } else {
              callback()
              // console.log('1')
            }
          }
        }],
        introduce: [{
          required: true,
          message: '部门介绍不能为空',
          trigger: 'blur'
        }, {
          trigger: 'blur',
          message: '部门介绍的长度应为1-100个字符',
          min: 2,
          max: 100
        }],
        managerId: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          }, {
            trigger: 'blur',
            message: '部门名称的长度为2-10个字符',
            min: 2,
            max: 10
          }, {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let res = await _getDepartmentList()
              console.log(res)
              if (this.formData.id) {
                res = res.filter(item => item.id !== this.formData.id)
              }
              if (res.some(item => item.name === value)) {
                callback(new Error('部门中已有该名字'))
              } else {
                callback()
              }
            }
          }
        ]
      }
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      // resetFields只能重置绑定的数据
      this.$refs.addDept.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      const res = await _getManagerList()
      this.managerList = res
    },
    btnAsure() {
      let msg = '新增'
      this.$refs.addDept.validate(async pass => {
        if (pass) {
          // 编辑场景下
          if (this.formData.id) {
            await _updateDepartment(this.formData)
            msg = '更新'
          } else { // 新增场景下
            await _addDepartment({ ...this.formData, pid: this.currentId })
          }
          this.$emit('updateDepartment')
          this.$message.success(`${msg}新增弹层成功`)
          this.close()
        }
      })
    },
    async getDepartmentDetail() {
      const res = await _getDepartmentDetail(this.currentId)
      this.formData = res
    }
  }
}
</script>

<style>

</style>
